/**
 * 用户树形选择
*/
Ext.namespace("Tree2");

Tree2.User=function(ids,fn,onlyOrg,roleId){
	this.fn=fn;
	this.ids=ids;
	this.result=new Array();
	this.winEdit = null;
	this.roleId=roleId;
	this.data={ids:this.ids};
	this.curNode=null;
	this.rootNode=null;
	if(onlyOrg)
		this.data['onlyOrg']=onlyOrg;
	if(roleId)
		this.data['roleId']=roleId;
	this.loader=new SysMgr.CheckLoader({
		url:'../sysmanager/treeBranchManager.form?snaptime=' + Date.parse(new Date()),
		listeners:{
				beforeload:this.expandBranch
			},
		baseParams:this.data
	});
	this.tree2 = new Ext.tree.TreePanel({
        width: 260,
        height:300,
        useArrows:true,
        autoScroll:true,
        animate:true,
        bodyStyle: 'background-color: #FFFFFF;',
        containerScroll: true,
        rootVisible: false,
        frame: true,
        loader:this.loader,
        listeners: {
            checkchange: this.onCheckChildren
            //dblclick: this.onEditRole
        },
        root: {
            id: '0',
			text: '用户选择',
			iconCls: 'icon-group',
			expanded:true
        },
        tbar:{items:[{
			text: '',
			iconCls: 'icon-new',
			handler: this.onAddRole,
			scope: this
        },{
			text: '',
			iconCls: 'icon-modify',
			handler: this.onEditRole,
			scope: this
        },{
            text: '',
            iconCls: 'icon-remove',
           	handler: this.onDeleteRole,
			scope: this
        }]}
    });
    
    Tree2.User.superclass.constructor.call(this,{
		title: '部门选择',
		autoHeight: true,
        width: 300,
        y:5,    
        resizable: false,
        plain: true,
        modal: true,
        autoScroll: true,
        closeAction: 'hide',
        items: [this.tree2],
		buttons:[{
			text: '确定',
			cls: 'x-btn-text-icon btn-accept',
			handler: this.setParent,
			scope: this
        },{
            text: '关闭',
            cls: 'x-btn-text-icon btn-cancel',
            handler: this.hide.createDelegate(this, [])
        }]
    });
}

Ext.extend(Tree2.User,Ext.Window,{
	initWin:function(){
		
	},
	onCheckChildren:function(node,checked){
		if(node.isLeaf())return;
		scope=this;
		if(node.expanded){
			Ext.each(node.childNodes, function(child){
	        	child.getUI().toggleCheck(checked);	            
	        });
		}else{
			node.expand(false,false,function(node){
				Ext.each(node.childNodes, function(child){
		        	child.getUI().toggleCheck(checked);	            
		        });
			});
		}
	},
	expandBranch:function(loader,node){
		if(node.id!='0'){
			loader.baseParams.branchType =node.attributes['value'];
		}
		loader.baseParams.id = node.id;		
		//alert(Ext.util.JSON.encode(loader.baseParams));
	},
	setParent:function(){
		scope=this;
		this.result=new Array();
        var selNodes = this.tree2.getChecked();
        Ext.each(selNodes, function(node){
        	pnode=node.parentNode;   
        	if(node.isLeaf()){        		
        		if(!scope.checkDuplicate(scope.result,node.id)){
	        		var value=node.attributes['value'];
		            var item={id:node.id,text:node.text,value:value,parentId:pnode.id,parentText:pnode.text};
		            scope.result.push(item);
        		}
        	}
        });
        this.hide();        
        this.fn(this.result);
    },
    checkDuplicate:function(result,id){
    	for(var i=0;i<result.length;i++){
			if(result[i].id == id){
				return true;
			}
		}
		return false;
    },
    onEditRole:function(node){
    	var node=this.tree2.getSelectionModel().getSelectedNode();
    	if(node==null||node.isLeaf()||node.attributes['value']=='1'){
    		Utils.info2("请选择要修改群组！",180);
    		return ;
    	}
    	if (this.winEdit != null) {
			this.winEdit.destroy();
		}
		this.curNode=node;
		var data={parentId:this.roleId,roleId:node.id,roleName:node.text};
		this.winEdit = new SysMgr.GroupEdit2(this,node.id,this.roleId);
		this.winEdit.show();		
		this.winEdit.initWin(data);
    },
    onDeleteRole:function(){
    	var node=this.tree2.getSelectionModel().getSelectedNode();
    	if(node==null||node.isLeaf()||node.attributes['value']=='1'){
    		Utils.info2("请选择要删除群组！",180);
    		return ;
    	}
    	if(node.id=='10101'||node.id=='10102'){
    		Utils.info2("公共群组无法删除！",180);
    		return ;
    	}
    	scope=this;
    	Utils.confirm2({
			msg:'删除后将不可恢复，你确实要删除吗?',
			width:250,
			fn:function(){
	       		scope.deleteRole(node)
	       }
	    });
    	
    },
    deleteRole:function(node){
    	scope=this;
    	Ext.Ajax.request({
		   url: '../sysmanager/deleteGroup.form?snaptime=' + Date.parse(new Date()),
		   params: {roleId:node.id},
		   success: function(response){
		   	Utils.info2();			
			scope.tree2.getLoader().load(scope.tree2.getRootNode());			
		   }
		}); 
    },
    onAddRole:function(){
    	if (this.winEdit != null) {
			this.winEdit.destroy();
		}
		var data={parentId:this.roleId};
		this.winEdit = new SysMgr.GroupEdit2(this,null,this.roleId);
		this.winEdit.show();		
		this.winEdit.initWin(data);
    }
});